package unilearn.generate;

import java.io.PrintWriter;
import java.sql.SQLException;

/*
 * GenerateAssignments.java
 *
 * Created on 29 April 2008, 22:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 * @author Ben
 */
public class GenerateAssignments
{

	public static final String INSERT_ASSIGNMENT = "INSERT INTO assignment (assignmentName,assignmentDescription,dueDate,acceptingSubmissions,releaseDate,subjectID,fileID) "
			+ "VALUES ('Assignment %1','This is example assignment #%1','2008-06-15',%3,'2008-04-01',%4,%5);";

	long assignmentsCreated = 0;

	private GenerateFileData fileGen;

	private GenerateSubmission submissionGen;

	private PrintWriter writer;

	/** Creates a new instance of GenerateAssignments */
	public GenerateAssignments(GenerateFileData fileGen, GenerateSubmission submissionGen,
			PrintWriter writer)
	{
		this.fileGen = fileGen;
		this.submissionGen = submissionGen;
		this.writer = writer;
	}

	public long addAssignments(long subjectID, String[] subjectStudents) throws SQLException
	{
		int assignments = (int) (Math.random() * 7.0);
		int noAccepted = (int) (Math.random() * assignments);
		for (int j = 0; j < assignments; j++)
		{
			assignmentsCreated++;

			long fileID = fileGen.addFileData("assignment", assignmentsCreated);

			String value = INSERT_ASSIGNMENT;
			value = value.replace("%1", String.valueOf(j + 1));
			if (j < noAccepted)
			{
				value = value.replace("%3", "0");
			}
			else
			{
				value = value.replace("%3", "1");
			}
			value = value.replace("%4", String.valueOf(subjectID)); // Subjects
			value = value.replace("%5", String.valueOf(fileID)); // Files
			writer.println(value);

			submissionGen.addSubmissions(assignmentsCreated, subjectStudents);
		}

		return assignmentsCreated;
	}

}
